// cloudfunctions/submitFeedback/index.js
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const { content, contact, userId } = event;
    
    // 参数验证
    if (!content) {
      return {
        success: false,
        message: '反馈内容不能为空'
      };
    }
    
    // 保存反馈信息
    const result = await db.collection('feedback')
      .add({
        data: {
          content: content,
          contact: contact,
          userId: userId,
          status: 0, // 0=未处理, 1=已处理
          createTime: db.serverDate(),
          updateTime: db.serverDate()
        }
      });
      
    return {
      success: true,
      message: '反馈提交成功',
      feedbackId: result._id
    };
    
  } catch (error) {
    console.error('提交反馈失败:', error);
    return {
      success: false,
      message: '提交反馈失败'
    };
  }
}